package com.mz.mattress.admin.api.merchant;

import com.mz.mattress.admin.common.Holder;
import com.mz.mattress.admin.service.MemberMerService;
import com.mz.mattress.api.common.Constants;
import com.mz.mattress.api.common.Result;
import com.mz.mattress.api.entity.MemberMer;
import com.mz.mattress.api.jwt.MerchantJwt;
import com.mz.mattress.api.req.UpdatePasswordReq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping(Constants.MERCHANT_PATH + "memberMer")
public class MemberMerController {

    @Autowired
    private MemberMerService memberMerService;

    @GetMapping(value = "/get")
    public Result<MemberMer> get() {
        MerchantJwt userJwt = Holder.MERCHANT_JWT_THREAD_LOCAL.get();
        log.info("memberMerId={}", userJwt.getMemberMerId());
        return memberMerService.getById(userJwt.getMemberMerId());
    }

//    @GetMapping(value = "/switchMerchantCreateToken")
//    public Result<String> switchMerchantCreateToken(@RequestParam Integer merchantId) {
//        MerchantJwt userJwt = Holder.MERCHANT_JWT_THREAD_LOCAL.get();
//        return memberMerService.switchMerchantCreateToken(userJwt.getMobile(), merchantId, userJwt.getMemberMerId());
//    }

    @PostMapping(value = "/updatePassword")
    public Result updatePassword(@RequestBody UpdatePasswordReq req) {
        MerchantJwt merchantJwt = Holder.MERCHANT_JWT_THREAD_LOCAL.get();
        req.setMemberId(merchantJwt.getMemberMerId());
        return memberMerService.updatePassword(req);
    }

    @PostMapping(value = "/resetPassword")
    public Result resetPassword() {
        MerchantJwt merchantJwt = Holder.MERCHANT_JWT_THREAD_LOCAL.get();
        return memberMerService.resetPassword(merchantJwt.getMemberMerId());
    }

    @GetMapping("delete")
    public Result delete() {
        MerchantJwt merchantJwt = Holder.MERCHANT_JWT_THREAD_LOCAL.get();
        Long memberMerId = merchantJwt.getMemberMerId();
        log.info("memberMerId={}", memberMerId);
        return memberMerService.deleteById(memberMerId);
    }

}
